package com.gxa.hotel.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.hotel.admin.service.GoodsOrderService;
import com.gxa.hotel.core.common.JsonResult;
import com.gxa.hotel.core.pojo.domain.GoodsOrder;
import com.gxa.hotel.core.pojo.domain.RoomOrder;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goodsOrder")
public class GoodsOrderController {

    @Autowired
    GoodsOrderService goodsOrderService;
    LambdaQueryWrapper<GoodsOrder> lambda= null;
    @ApiOperation(value = "商品订单首页分页查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),


    })
    @PostMapping("/orderPage")
    public Map selectByOrderPage(
            @RequestParam(value = "page",required = true)Integer page,
            @RequestParam(value = "size",required = true)Integer size
    ){

        Page<GoodsOrder> page1 = new Page<>(page, size);
        Page<GoodsOrder> result = goodsOrderService.page(page1);

        JsonResult jsonResult=new JsonResult();

        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        //数据
        jsonResult.setData(result.getRecords());
        //总记录数
        jsonResult.set("count",result.getTotal());
        return jsonResult.getMap();
    }
    @ApiOperation(value = "商品订单动态条件查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),
            @ApiImplicitParam(name = "state",value = "订单状态",paramType = "query"),
            @ApiImplicitParam(name = "from",value = "起始时间",paramType = "query"),
            @ApiImplicitParam(name = "to",value = "截止时间",paramType = "query"),
            @ApiImplicitParam(name = "orderId",value = "订单编号",paramType = "query"),
            @ApiImplicitParam(name = "name",value = "收货人姓名",paramType = "query"),
            @ApiImplicitParam(name = "phone",value = "收货人电话",paramType = "query"),

    })
    @PostMapping("/selectByName")
    public Map selectByName(
            @RequestParam(value = "state",required = false)Integer state,
            @RequestParam(value = "from",required = false)String from,
            @RequestParam(value = "to",required = false)String to,
            @RequestParam(value = "orderId",required = false)Integer orderId,
            @RequestParam(value = "name",required = false)String name,
            @RequestParam(value = "phone",required = false)String phone,
            @RequestParam(value = "page",required = true)Integer page,
            @RequestParam(value = "size",required = true)Integer size
    ){

        String static1= String.valueOf(state);
        String orderId1=String.valueOf(orderId);
        if (state==null){
            static1="";
        }
        if (orderId==null){
            orderId1="";
        }
        lambda= Wrappers.<GoodsOrder>lambdaQuery();
        lambda.eq(StringUtils.isNotBlank(static1),GoodsOrder::getGoState,state)
                .gt(StringUtils.isNotBlank(from),GoodsOrder::getGoSubmitTime,from)
                .lt(StringUtils.isNotBlank(to),GoodsOrder::getGoSubmitTime,to)
                .like(StringUtils.isNotBlank(orderId1),GoodsOrder::getGoAid,orderId)
                .like(StringUtils.isNotBlank(name),GoodsOrder::getGoName,name)
                .like(StringUtils.isNotBlank(phone),GoodsOrder::getGoPhone,phone);
        Page<GoodsOrder> page1 = new Page<>(page, size);
        Page<GoodsOrder> result = goodsOrderService.page(page1,lambda);

        JsonResult jsonResult=new JsonResult();

        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        //数据
        jsonResult.setData(result.getRecords());
        //总记录数
        jsonResult.set("count",result.getTotal());
        return jsonResult.getMap();
    }

    @ApiOperation(value = "删除订单",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "删除订单号",paramType = "query"),



    })
    @PostMapping("/deleteById")
    public Map deleteById(
            @RequestParam(value = "id",required = true)String id

    ){
        lambda= Wrappers.<GoodsOrder>lambdaQuery();
        lambda.eq(GoodsOrder::getGoAid,id);

        boolean remove = goodsOrderService.remove(lambda);

        JsonResult jsonResult=new JsonResult();
        if (remove){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }


        return jsonResult.getMap();
    }
    @ApiOperation(value = "修改订单状态",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "修改订单号",paramType = "query"),

    })
    @PostMapping("/updateByStart")
    public Map updateByStart(
            @RequestParam(value = "id",required = true)String id,
            @RequestParam(value = "start",required = true)Integer start
    ){
        lambda= Wrappers.<GoodsOrder>lambdaQuery();
        lambda.eq(GoodsOrder::getGoAid,id);
        GoodsOrder goodsOrder = goodsOrderService.getOne(lambda);
        goodsOrder.setGoState(start);
        boolean b = goodsOrderService.saveOrUpdate(goodsOrder);

        JsonResult jsonResult=new JsonResult();
        if (b){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }


        return jsonResult.getMap();
    }
    @ApiOperation(value = "查看订单",produces = "query")

    @PostMapping("/selectAll")
    public Map selectAll(){

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }
}
